package cn.edu.xcu.ckl;

import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.lzy.okgo.OkGo;

import cn.edu.xcu.ckl.api.ApiConstants;
import cn.edu.xcu.ckl.base.BaseActivity;
import cn.edu.xcu.ckl.entity.UserInfo;
import cn.edu.xcu.ckl.http.HttpStringCallback;
import cn.edu.xcu.ckl.utils.GsonUtils;


public class LoginActivity extends BaseActivity {

    private EditText et_username;
    private EditText et_password;
    private CheckBox checkbox;

    private SharedPreferences mSharedPreferences;
    private boolean is_login;



    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        //初始化控件
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        checkbox = findViewById(R.id.checkbox);
    }

    @Override
    protected void setListener() {

        //注册
        findViewById(R.id.register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mBaseActivity,RegisterActivity.class));
            }
        });

        //登录点击事件
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = et_username.getText().toString();
                String password = et_password.getText().toString();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    showToast("请输入用户名和密码");
                } else {

                    //保存用户名和密码
                    SharedPreferences.Editor edit = mSharedPreferences.edit();
                    edit.putBoolean("is_login", is_login);
                    edit.putString("username", username);
                    edit.putString("password", password);
                    //这句话不能少
                    edit.commit();
                    login(username, password);
                }
            }
        });

        //记住密码监听事件
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_login =isChecked;
            }
        });

    }

    @Override
    protected void initData() {
        //获取mSharedPreferences实例
        mSharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
        //是否勾选了记住密码
        is_login = mSharedPreferences.getBoolean("is_login", false);
        if (is_login) {
            String username = mSharedPreferences.getString("username", null);
            String password = mSharedPreferences.getString("password", null);
            et_username.setText(username);
            et_password.setText(password);
            checkbox.setChecked(true);

        }
    }

    /**
     * 登录
     */
    private void login(String username, String password) {
        OkGo.<String>get(ApiConstants.LOGIN_URL)
                .params("username", username)
                .params("password", password)
                .execute(new HttpStringCallback(mBaseActivity) {
                    @Override
                    protected void onSuccess(String msg, String response) {
                        UserInfo userInfo = GsonUtils.parseJson(response, UserInfo.class);
                        UserInfo.setUserInfo(userInfo);
                        finish();

                    }

                    @Override
                    protected void onError(String response) {
                        showToast(response);
                    }
                });


    }
}